package com.example.webpractice.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.webpractice.dao.KechengxinxiDao;
import com.example.webpractice.dto.KechengxinxiDTO;
import com.example.webpractice.po.KechengxinxiPO;
import com.example.webpractice.vo.KechengxinxiDetailVO;
import com.example.webpractice.vo.KechengxinxiListVO;
import com.example.webpractice.vo.PageVo;
import com.example.webpractice.vo.Po2Vo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("kechengxinxiService")
public class KechengxinxiService {

    @Autowired
    private KechengxinxiDao dao;

    public PageVo queryPage(int pages, int limit, QueryWrapper queryWrapper) {
        Page<KechengxinxiPO> pagePO = dao.selectPage(new Page<>(pages, limit), queryWrapper);
        return Po2Vo.toVoPage(pagePO, KechengxinxiListVO.class);
    }

    public KechengxinxiDetailVO selectById(Long id) {
        KechengxinxiPO po = dao.selectOne(new QueryWrapper<KechengxinxiPO>().eq("id", id));
        KechengxinxiDetailVO vo = new KechengxinxiDetailVO();
        BeanUtils.copyProperties(po, vo);
        return vo;
    }


    public int deleteBatchIds(List list) {
        return dao.deleteByIds(list);
    }


    public int update(KechengxinxiDTO kechengxinxiDTO) {
        KechengxinxiPO po = new KechengxinxiPO();
        BeanUtils.copyProperties(kechengxinxiDTO, po);
        return dao.updateById(po);
    }


    public int create(KechengxinxiDTO kechengxinxiDTO) {
        KechengxinxiPO po = new KechengxinxiPO();
        BeanUtils.copyProperties(kechengxinxiDTO, po);
        return dao.insert(po);
    }

    public KechengxinxiDetailVO selectByName(String name) {
        QueryWrapper queryWrapper = new QueryWrapper<KechengxinxiPO>();
        queryWrapper.eq("kechengmingcheng", name);
        KechengxinxiPO po = dao.selectOne(queryWrapper);
        if (po == null) {
            return null;
        }
        KechengxinxiDetailVO vo = new KechengxinxiDetailVO();
        BeanUtils.copyProperties(po, vo);
        return vo;
    }

}
